package cn.stronglink.asset.message.mq.dam;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.jms.Message;
import javax.jms.MessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cn.stronglink.asset.module.dam.entity.message.MsgRealTimeDatas;
import cn.stronglink.asset.module.dam.interfaces.RealtimeDatasHandleStrategyI;
import cn.stronglink.asset.module.dam.service.RealTimeSendToDoorDb;
import cn.stronglink.asset.module.dam.service.RealTimeSendToSenDb;
import cn.stronglink.asset.module.dam.service.RealTimeSendCmdToDam;
import cn.stronglink.asset.module.dam.utils.dataJsonUtils.realtime.receive.IntercepterRealTimeDatasUtil;

/**
 * 监听Mq: damToweb消息, 调用内部线程类:处理数据
 */
@Component("realTimemsgReceive")
public class RealTimeDatasMsgReceiver  implements MessageListener {
	protected final Logger logger = LoggerFactory.getLogger(RealTimeDatasMsgReceiver.class);
	private List<RealtimeDatasHandleStrategyI> strategys = new ArrayList<RealtimeDatasHandleStrategyI>();
	private ExecutorService threadPool = Executors.newFixedThreadPool(100);// 线程数为100；

	// 屏幕dam更新： 6个传感器数据   其他常量机柜信息
	@Autowired
	private RealTimeSendCmdToDam toDamCmd6Service;
	
	// 数据库db更新
	@Autowired
	private RealTimeSendToSenDb toSensorDbService;
	@Autowired
	private RealTimeSendToDoorDb toDoorDbService;

	@Override
	public void onMessage(Message message) {
		if (!IntercepterRealTimeDatasUtil.isDataGood(message, 500))
			return; // 两次门数据：1s间隔

		if (strategys.isEmpty()) {
			strategys.add(toSensorDbService);
			strategys.add(toDoorDbService);
			strategys.add(toDamCmd6Service);
		}

		threadPool.execute(new Runnable() {
			@Override
			public void run() {
				handle(IntercepterRealTimeDatasUtil.array);
			}
		});
	}

	// 逻辑处理
	public void handle(MsgRealTimeDatas[] array) {

		for (RealtimeDatasHandleStrategyI ser : strategys) {
			for (MsgRealTimeDatas data : IntercepterRealTimeDatasUtil.array) {

				if (data != null && ser.isHandle(data)) {
					// super.excuteThread(new ThreadForReceiveUtil(realDatas, ser));
					ser.handleDamDatas(data);
				}
			}
		}
	}
}
